package com.bingxue.edu.management.shop.controller;

import com.bingxue.edu.framework.common.model.Response;
import com.bingxue.edu.framework.common.model.SortQuery;
import com.bingxue.edu.framework.mybatis.query.QueryBuilder;
import com.bingxue.edu.framework.security.ApiResource;
import com.bingxue.edu.management.shop.model.entity.FinAccount;
import com.bingxue.edu.management.shop.model.query.FinAccountQuery;
import com.bingxue.edu.management.shop.model.req.FinAccountCreateReq;
import com.bingxue.edu.management.shop.model.req.FinAccountUpdateReq;
import com.bingxue.edu.management.shop.model.resp.FinAccountResp;
import com.bingxue.edu.management.shop.service.FinAccountService;
import com.mybatisflex.core.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name = "财务帐户接口")
@RestController
@RequestMapping("/shop/fin-account")
@RequiredArgsConstructor
public class FinAccountController {
    private final FinAccountService finAccountService;

    @Operation(summary = "查询财务账户列表" , description = "查询财务账户列表")
//    @ApiResource("system:fin-account:manage")
    @GetMapping("/list")
    public Response<List<FinAccountResp>> finAccountList(FinAccountQuery query, SortQuery sortQuery) {
        QueryWrapper queryWrapper = QueryBuilder.build(query, sortQuery);
        List<FinAccountResp> finAccountRespList = finAccountService.listAs(queryWrapper, FinAccountResp.class);
        return Response.success(finAccountRespList);
    }

    @Operation(summary = "新增财务账户" , description = "新增财务账户")
    @ApiResource("system:fin-account:manage")
    @PostMapping("/create")
    public Response<Void> createFinAccount(@Validated @RequestBody FinAccountCreateReq req) {
        FinAccount finAccount = finAccountService.copy(req);
        finAccountService.save(finAccount);
        return Response.success();
    }

    @Operation(summary = "删除财务账户" , description = "删除财务账户")
    @ApiResource("system:fin-account:manage")
    @PostMapping("/delete/{ids}")
    public Response<Void> deleteFinAccount(@PathVariable List<String> ids) {
        finAccountService.remove(ids);
        return Response.success();
    }

    @Operation(summary = "查询财务账户" , description = "查询财务账户")
//    @ApiResource("system:fin-account:manage")
    @GetMapping("/detail/{id}")
    public Response<FinAccountResp> finAccountDetail(@PathVariable String id) {
        FinAccountResp finAccountResp = finAccountService.getMapper().selectOneWithRelationsByIdAs(id,FinAccountResp.class);
        return Response.success(finAccountResp);
    }

    @Operation(summary = "修改财务账户" , description = "修改财务账户")
    @ApiResource("system:fin-account:manage")
    @PostMapping("/update/{id}")
    public Response<Void> updateFinAccount(@PathVariable String id,@RequestBody FinAccountUpdateReq req) {
        FinAccount finAccount = finAccountService.copyById(req, id);
        finAccountService.updateById(finAccount);
        return Response.success();
    }
}
